import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:shadcn_ui/shadcn_ui.dart';
import 'package:yuellyfactory/eventbus/EventBusUtil.dart';
import 'package:yuellyfactory/eventbus/EventRefreshBase.dart';
import 'package:yuellyfactory/util/ToastUtil.dart';

import '../../util/ColorUtil.dart';
import '../../util/ViewUtil.dart';
import '../Controller/RuleController.dart';
import '../dto/PriceParm.dart';
import '../dto/PricePart.dart';
import 'RuleView.dart';


class EditChildParmRule extends StatefulWidget {

  final PricePart pricePart;
  final Function callback;
  final PriceParm priceParm;
  const EditChildParmRule({super.key, required this.pricePart, required this.callback, required this.priceParm});

  @override
  State<EditChildParmRule> createState() => _EditChildParmRuleState();
}

class _EditChildParmRuleState extends State<EditChildParmRule> {

  final RuleController ruleController=RuleController();
  PriceParm? priceParm;

  List settingRules=[];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    priceParm=widget.priceParm;
    ruleController.settingRules=(List data){
      debugPrint("回调");
      settingRules=data;
    };
  }
  @override
  Widget build(BuildContext context) {
    return ShadSheet(
      onClosing: (){
        debugPrint("关闭回调");
        widget.callback();
      },
      constraints: const BoxConstraints(minWidth: 800,maxWidth: 800),
      title: Row(
        children: [
          const Text('零件公式编辑'),
          const SizedBox(width: 50,),
          ViewUtil.doneIcon("保存", (){
            submit();
          })
        ],
      ),
      child: Column(
        children: [
          RuleView(widget.pricePart, ruleController,priceParm!,type: 1,),
          const SizedBox(height: 16),

        ],
      ),
    );
  }

  void submit(){
    if(settingRules.isEmpty){
      ToastUtil.showToast("请编辑公式");
      return;
    }
    var datas=json.encode(settingRules);
    widget.callback(datas);
    Navigator.pop(context);

  }
}
